package com.my.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.my.bean.*;
import com.my.mapper.InventoryMapper;
import com.my.service.InventoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.my.service.LossService;
import com.my.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存盘点 服务实现类
 * </p>
 *
 * @author 崔倩倩
 * @since 2018-08-26
 *
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {
    @Autowired
    LossService lossService;
    @Autowired
    StockService stockService;

    @Override
    public ResultPage<Inventory> selectMyPage(Inventory inventory, VoPage voPage,long areaid,boolean admin) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<Inventory> list = baseMapper.selectMyPage(paramMap,inventory,areaid,admin);
        Integer count = baseMapper.countMyPage(paramMap,inventory,areaid,admin);
        ResultPage<Inventory> resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public String insertInventory(String param,long areaid) throws SQLException {
        Inventory inventory= JSONObject.parseObject(param,Inventory.class);
        inventory.setDate(new Date().getTime());
        inventory.setAreaid(areaid);
        if(this.insert(inventory)){
            Map map=new HashMap<>();
            map.put("item_name",inventory.getItemName());
            List<Stock> stocks= stockService.selectByMap(map);
            Loss loss=new Loss();
            loss.setDate(new Date().getTime());
            loss.setItemName(inventory.getItemName());
            loss.setSpecifications(inventory.getSpecifications());
            if (stocks.size()>0){
                double lossNum=inventory.getNumber()- stocks.get(0).getNumber();
                if (lossNum>0){
                    loss.setLossNum(lossNum+"" );
                    //标识 这是多出的 用于前台 四舍五入
                    loss.setFlag(1);
                }
                if (lossNum<0){
                    lossNum=stocks.get(0).getNumber()-inventory.getNumber();
                    loss.setLossNum(""+ lossNum );
                    //标识 这是损耗的 用于前台 四舍五入
                    loss.setFlag(2);
                }
                if (lossNum==0){
                    lossNum=stocks.get(0).getNumber()-inventory.getNumber();
                    loss.setLossNum(""+ lossNum );
                    //标识 这是无损耗的 用于前台 四舍五入
                    loss.setFlag(3);
                }
                loss.setDate(new Date().getTime());
                loss.setAreaid(areaid);
                if ( lossService.insert(loss)){
                    return  "新增成功";
                }
            }else{
                loss.setLossNum(""+inventory.getNumber());
                //标识 这是多出的 用于前台 四舍五入
                loss.setFlag(1);
                if ( lossService.insert(loss)){
                    return  "新增成功";
                }
            }

        }
        throw  new SQLException();

    }
}
